﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Xml;
using OffInvoiceExcelTemplate;

namespace OffInvoiceExcelTemplate
{
    public class FieldFormats
    {
        string language;
        string languajePath;
        public string messagePath;
        CultureInfo culture;
        NumberFormatInfo num;
        Hoja1 h1;

        public FieldFormats(CultureInfo culture, NumberFormatInfo num, string language)
        {
            this.culture = culture;
            this.num = num;
            this.language = language;

            languajePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft OffInvoice\Languages\" + language + ".xml";
            messagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft OffInvoice\Messages\" + language + ".xml";
            h1 = Globals.Hoja1;
        }

        public string description_Validating(string description)
        {
            if (string.IsNullOrEmpty(description))
            {
                h1.d_description.Select();
                throw new Exception("ErrorMandatoryDescription");
            }

            return description;
        }

        public string ud_Validating(string units)
        {
            try
            {
                
                double mVar = double.Parse(units, num);
                if (mVar <= 0)
                {

                    throw new Exception();
                }
                units = string.Format(num, "{0:#,0.00}", mVar);
            }
            catch (Exception)
            {
                h1.d_ud.Select();
                throw new Exception("ErrorUnlegalLiteralsUd");
            }

            return units;
        }

        public string discount_Validating(string disc)
        {
            try
            {
                if (!string.IsNullOrEmpty(disc))
                {
                    double xVar = double.Parse(disc, num);
                    if (xVar < 0)
                    {
                        throw new Exception("negativeDiscountRate");
                    }
                    else
                    {
                        disc = string.Format(num, "{0:#,0.00}", xVar);
                    }
                }
            }
            catch (Exception ex)
            {
                h1.d_disc.Select();
                if (ex.Message.Equals("negativeDiscountRate"))
                {

                    throw new Exception("ErrorNegativeDiscount");
                }
                else
                {
                    throw new Exception("ErrorUnlegalLiteralsDesc");
                }
            }

            return disc;
        }

        public string generalDiscount_Validating(string disc)
        {
            try
            {
                if (!string.IsNullOrEmpty(disc))
                {
                    double xVar = double.Parse(disc, num);
                    if (xVar < 0)
                    {
                        throw new Exception("negativeDiscountRate");
                    }
                    else
                    {
                        disc = string.Format(num, "{0:#,0.00}", xVar);
                    }
                }
                else
                {
                    throw new Exception("ErrorMandatoryGeneralDiscountRate");
                }

            }
            catch (Exception ex)
            {
                h1.d_discRate.Select();
                if (ex.Message.Equals("negativeDiscountRate"))
                {
                    throw new Exception("ErrorNegativeDiscount");
                }
                else
                {
                    throw new Exception("ErrorUnlegalLiteralsDesc");
                }
            }

            return disc;
        }

        public string price_Validating(string price)
        {
            try
            {
                double xVar = double.Parse(price, num);
                if (xVar <= 0)
                {
                    throw new Exception();
                }
                price = string.Format(num, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {
                h1.d_price.Select();
                throw new Exception("ErrorUnlegalLiteralsPrice");

            }

            return price;
        }

        public string iva_Validating(string IVA)
        {
            try
            {
                double xVar = double.Parse(IVA, num);
                if (xVar < 0)
                {
                    throw new Exception();
                }
                IVA = string.Format(num, "{0:#,0.00}", xVar);

            }
            catch (Exception)
            {
                h1.d_iva.Select();
                throw new Exception("ErrorUnlegalLiteralsIVA");

            }

            return IVA;
        }

        public string irpf_Validating(string IRPF)
        {
            try
            {
                double xVar = double.Parse(IRPF, num);
                if (xVar < 0)
                {
                    throw new Exception();
                }
                IRPF = string.Format(num, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {
                h1.d_irpf.Select();
                throw new Exception("ErrorUnlegalLiteralsIRPF");

            }

            return IRPF;
        }

        public string LoadLabel(string path, string tag)
        {
            string label = " ";
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);
                string lb = xmldoc.GetElementsByTagName(tag).Item(0).InnerText;
                if (!string.IsNullOrEmpty(lb))
                    label = lb;

                return label;

            }
            catch (Exception)
            {
                return label;
            }
        }

        public string twoDecimalFormat(string value)
        {
            try
            {
                double xVar = double.Parse(value, num);
                value = string.Format(num, "{0:#,0.00}", xVar);
            }
            catch (Exception)
            {

            }

            return value;
        }

    }
}
